<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog
      :close-on-click-modal="false"
      :title="mytitle"
      :modal-append-to-body="false"
      :visible.sync="visible"
      :show="show"
      :before-close="closeDialog"
      @close="$emit('update:show',false)"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" class="mar_top" label-width="100px">
        <el-form-item label="配图">
          <el-form class="clearfix" label-position="right" label-width="120px">
            <el-col :span="24" class="mar_bottom_20">
              <el-col :span="24">
                <el-upload
                  class="setZhutu"
                  action="/api/common/upload/index/"
                  list-type="picture-card"
                  :file-list="fileList"
                  :on-success="uploadSuccess"
                  :on-preview="handlePictureCardPreview"
                  :before-remove="beforeRemove"
                  :on-remove="handleRemove"
                >
                  <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :close-on-click-modal="false" :visible.sync="dialogVisible">
                  <img width="60%" :src="dialogImageUrl" alt />
                </el-dialog>
              </el-col>
            </el-col>
          </el-form>
        </el-form-item>

        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="subForm('data')"
          >确定</el-button>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="initPic()"
          >更新</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped></style>

<script>
export default {
  data() {
    return {
      mytitle: "",
      updateUrl: "",
      showImg: true,
      loading: false,
      visible: this.show,
      dialogVisible: false,
      dialogImageUrl: "",
      data: {},
      fileList: []
    };
  },
  props: {
    show: Boolean,
    editId: String,
    clickNum: Number
  },
  methods: {
    uploadSuccess(response, file, fileList) {
      if (response.code == 200 && response.code_desc == "请求成功") {
        this.$message.success("上传成功！");
        console.log(file);
        this.fileList = fileList;
      } else if (response.code == 400) {
        this.$message.error("上传失败！不支持的类型");
      }
    },
    handleRemove(file, fileList) {
      var id = file.id || "0";
      if (id == "0") {
        this.fileList = fileList;
      } else {
        var params = {};
        params.id = id;
        this.$api.getShops.delReviewPic(params).then(res => {
          if (res.data.code == 200 && res.data.result) {
            //this.$fnc.alertSuccess({text:'修改成功'});  //成功弹出框
            this.fileList = fileList;
          } else {
            this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
          }
        });
      }
    },
    beforeRemove(file, fileList) {
      console.log(file);
      return this.$confirm(`确定删除吗？`);
    },
    handlePictureCardPreview(file) {
      this.dialogVisible = true;
      this.dialogImageUrl = file.url;
      if (file.id) {
        // this.shopInfo.piclink=file.url;
      } else {
        // this.shopInfo=file.response.result;
      }

      // this.dialogImageUrl =this.$fnc.getImgUrl(file.url);
      // this.dialogVisible = true;
    },
    uploadError(response) {
      if ((response.code = 400)) {
        this.$message.error("无效请求");
      }
    },

    closeDialog() {
      this.visible = false;
    },

    subForm(formName) {
      console.log(this.fileList);
      var imgArr = [];
      for (var i in this.fileList) {
        if (!this.fileList[i].id) {
          //上传没有ID的
          imgArr.push(this.fileList[i].response.result);
        }
      }
      var img_json = JSON.stringify(imgArr);
      console.log(img_json); //["resource/pic1.jpg","resource/pic2.jpg"]

      var params = {};
      params["img_json"] = img_json;
      params["rid"] = 100;
      this.$api.getShops.updateReviewPic(params).then(res => {
        if (res.data.code == 200 && res.data.result != null) {
          this.$emit("parent", "这里刷新父页面");
          this.$fnc.alertSuccess({ text: "编辑成功" }); //成功弹出框
          this.visible = false;
        } else {
          this.$fnc.alertError(res.data.result); //失败弹出框
        }
      });
      /* 附PHP 控制器更新评论图片方法 */
      // public function up_review_pic(){
      // 	$review_pic_M = new \app\model\product_review_pic();
      // 	$rid = post('rid');
      // 	$img_json = post('img_json');
      // 	$ar = json_decode($img_json,true);
      // 	$res = $review_pic_M->up($rid,$ar);
      // 	empty($res) && error('修改失败',404);
      // 	return $res;
      //  }
      ////模型更新
      // public function up($rid,$data=[]){
      // $this->delete($this->title,['rid'=>$rid]);
      // foreach($data as $one){
      // $this->insert($this->title,['rid'=>$rid,'piclink'=>$one]);
      // }
      // return $this->doo();
      // }
    },
    initPic() {
      console.log(this.fileList);
      var rid = 100;
      var params = {};
      params["rid"] = rid;
      this.$api.getShops.findReviewPic(params).then(res => {
        var imgList = res.data.result;
        console.log(imgList);
        //var imgList = [{'id':1,'piclink':"resource/image/default/201903/445ee129f42e7b9c8112a553a1afb3e3.jpg"},{'id':2,'piclink':"resource/image/default/201903/6c4189fab093fcf3c465c2a131a90bd7.jpg"}];
        this.fileList.length = 0;
        for (var i in imgList) {
          var imgObj = {};
          imgObj.id = imgList[i].id;
          imgObj.url = this.$fnc.getImgUrl(imgList[i].piclink);
          this.fileList.push(imgObj);
        }
        console.log(this.fileList);
      });
    }
  },
  watch: {
    show() {
      console.log(this.show);
      this.visible = this.show;
    },
    clickNum(id) {
      console.log("点击button次数", id);
      console.log("编辑ID=" + this.editId);
      if (this.editId !== undefined) {
        this.mytitle = "查看评价";
        // this.getDetails(this.editId);
      } else {
        this.mytitle = "添加评价";
        if (this.$refs["data"] !== undefined) {
          this.$refs["data"].resetFields();
        }
        this.updateUrl = "";
        this.showImg = false;
      }
    }
  },

  mounted() {
    this.initPic();
  },
  created() {
    this.initPic();
  },
  updated() {}
};
</script>
